package com.czh.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czh.mybatisplus.entity.Mall;
import com.czh.mybatisplus.mapper.MallMapper;
import com.czh.mybatisplus.service.IMallService;
import com.czh.mybatisplus.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2024-09-05
 */
@Api(tags = "商城模块")
@RestController
@RequestMapping("/mall")
public class MallController {

    @Resource
    private IMallService mallService;

    @Resource
    private MallMapper mapper;

    @ApiOperation(value = "商城数据",notes = "")
    @PostMapping("all")
    public Result<Map<String,Object>> all(){
        List<Mall> list = mallService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("malls",list);
        return Result.success(map);
    }

    @ApiOperation(value = "insert(Entity)",notes = "新增（插入一条记录")
    @PostMapping("insert")
    public Result insert(@RequestBody Mall mall){
        int i = mapper.insert(mall);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "delete(Wrapper<T>)",notes = "删除（根据ID 删除")
    @PostMapping("delete")
    public Result delete(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Mall::getId,id);
        int i = mapper.delete(queryWrapper);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "deleteBatchIds(List<Integer>)",notes = "删除（根据ID 批量删除")
    @PostMapping("deleteBatchIds")
    public Result deleteBatchIds(@RequestBody List<Integer> idList){
        int i = mapper.deleteBatchIds(idList);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "deleteById(Integer)",notes = "删除（根据ID 删除")
    @PostMapping("deleteById")
    public Result deleteById(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        int i = mapper.deleteById(id);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "deleteByMap(Map<String,Object>)",notes = "删除（根据Map<字段名,值>条件，删除一条记录")
    @PostMapping("deleteByMap")
    public Result deleteByMap(@RequestBody Map<String,Object> map){
        int i = mapper.deleteByMap(map);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "update(Entity,Wrapper<T>)",notes = "修改（根据 Wrapper<T> 条件，更新记录")
    @PostMapping("update")
    public Result update(@RequestBody Mall mall){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Mall::getId,mall.getId());
        int i = mapper.update(mall,queryWrapper);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "updateById(Entity)",notes = "修改（根据 ID 修改")
    @PostMapping("updateById")
    public Result updateById(@RequestBody Mall mall){
        int i = mapper.updateById(mall);
        return Result.success(i>0?true:false);
    }

    @ApiOperation(value = "selectById(Integer)",notes = "查询（根据 ID 查询")
    @PostMapping("selectById")
    public Result selectById(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        Mall mall = mapper.selectById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("mall",mall);
        return Result.success(map);
    }

    @ApiOperation(value = "selectOne(Wrapper<T>)",notes = "查询（根据 Wrapper<T> “id”条件，查询“一条一条一条”记录")
    @PostMapping("selectOne")
    public Result selectOne(@RequestParam(value = "id",required = false,defaultValue = "") Integer id){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Mall::getId,id);
        Mall mall = mapper.selectOne(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("mall",mall);
        return Result.success(map);
    }

    @ApiOperation(value = "selectBatchIds(List<Integer>)",notes = "查询（根据ID 批量查询")
    @PostMapping("selectBatchIds")
    public Result selectBatchIds(@RequestBody List<Integer> idList){
        List<Mall> list = mapper.selectBatchIds(idList);
        Map<String,Object> map = new HashMap<>();
        map.put("malls",list);
        return Result.success(map);
    }

    @ApiOperation(value = "selectList(Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询全部记录")
    @PostMapping("selectList")
    public Result selectList(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        List<Mall> list = mapper.selectList(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("malls",list);
        return Result.success(map);
    }

    @ApiOperation(value = "selectByMap(Map<String,Object>)",notes = "查询（根据 Map<字段名,值> 条件查询多条）")
    @PostMapping("selectByMap")
    public Result<Map<String,Object>> selectByMap(@RequestBody Map<String,Object> map){
        List<Mall> list = mapper.selectByMap(map);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("malls",list);
        return Result.success(map2);
    }

    @ApiOperation(value = "selectMaps(Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询全部记录）")
    @PostMapping("selectMaps")
    public Result<Map<String,Object>> selectMaps(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        List<Map<String,Object>> list = mapper.selectMaps(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("malls",list);
        return Result.success(map);
    }

    @ApiOperation(value = "selectObjs(Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询全部记录。注意： 只返回第一个字段的值）")
    @PostMapping("selectObjs")
    public Result<Map<String,Object>> selectObjs(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        List<Object> list = mapper.selectObjs(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("mallIds",list);
        return Result.success(map);
    }

    @ApiOperation(value = "selectPage(IPage<T>,Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询全部记录（并分页1/5））")
    @PostMapping("selectPage")
    public Result<Map<String,Object>> selectPage(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        Page page = new Page<>(1, 5);
        mapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("malls",page);
        return Result.success(map);
    }

    @ApiOperation(value = "selectMapsPage(IPage<T>,Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询全部记录（并分页1/5））")
    @PostMapping("selectMapsPage")
    public Result<Map<String,Object>> selectMapsPage(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        Page page = new Page<>(1, 5);
        mapper.selectMapsPage(page, queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("malls",page);
        return Result.success(map);
    }

    @ApiOperation(value = "selectCount(Wrapper<T>)",notes = "查询（根据 Wrapper<T> 条件，查询总记录数）")
    @PostMapping("selectCount")
    public Result<Map<String,Object>> selectCount(@RequestParam(value = "name",required = false,defaultValue = "") String name){
        LambdaQueryWrapper<Mall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Mall::getName,name);
        Long count = mapper.selectCount(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("mallCount",count);
        return Result.success(map);
    }
}
